//
//  XYMainViewController.swift
//  01-新浪微博
//
//  Created by 陈许亚 on 16/3/6.
//  Copyright © 2016年 cxy. All rights reserved.
//

import UIKit

class XYMainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        addChildViewControllers()
        
        //自定义tabBar
        let mainTabbar = XYMainTabBar()
        mainTabbar.plusBtn.addTarget(self, action: "plusBtnDidClick", forControlEvents: .TouchUpInside)
        print(tabBar.classForCoder)
        setValue(mainTabbar, forKey: "tabBar")//自定义
        print(tabBar.classForCoder)
        

    }
    //plusBtn点击事件
    @objc private func plusBtnDidClick(){
        let composeVC = XYComposeViewController()
        let nav = UINavigationController(rootViewController: composeVC)
        presentViewController(nav, animated: true, completion: nil)
        
    }
    
    private func addChildViewControllers(){
        addChildViewController(XYHomeTableViewController(), title: "主页", imageName: "tabbar_home")
        
        addChildViewController(XYMessageTableViewController(), title: "信息", imageName: "tabbar_message_center")
        
        addChildViewController(XYDiscoverTableViewController(), title: "发现", imageName: "tabbar_discover")
        
        addChildViewController(XYProfileTableViewController(), title: "我", imageName: "tabbar_profile")
        
        
    }
    
    //MARK:添加子视图控制器
    private func addChildViewController(vc: UIViewController,title: String,imageName: String) {
        //导航栏的标题
        vc.navigationItem.title = title
        //tabBar的小标题
        vc.tabBarItem.title = title
        //tabBar的小标题的字体颜色
        vc.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.orangeColor()], forState: .Normal)
        
       tabBar.tintColor = UIColor.orangeColor()
        //tabBar的图片
        vc.tabBarItem.image = UIImage(named: imageName)
        //图片不被渲染
        vc.tabBarItem.selectedImage = UIImage(named: imageName + "_selected")?.imageWithRenderingMode(.AlwaysOriginal)
        //设置当行控制器的根视图控制器
        let nav = UINavigationController(rootViewController: vc)
        
        addChildViewController(nav)
        
        
    }
    

    
}










